#include "draw_objects.h"
#include <ostream>

/**
 * @brief  Draw objects on video
 * @param  bgr_frame
 * @param  objects
 */
int draw_objects_video(cv::Mat &image, const std::vector<Object> &objects,
                       bool detect_jack, int emailflag) {
  static const char *class_names[] = {"Drowning", "Person out of water",
                                      "Swimming"};
  static const char *class_names_jack[] = {"notwear", "wear"};
  // email flag
  int flag = 0;

  if (image.empty()) {
    return 0;
  }

  for (size_t i = 0; i < objects.size(); i++) {
    const Object &obj = objects[i];

    cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));

    char text[256];
    if (detect_jack) {
      sprintf(text, "%s %.1f%%", class_names_jack[obj.label], obj.prob * 100);
    } else {
      if (emailflag == 1 && obj.label == 0) {
        flag = 1;
      }
      sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
    }

    int baseLine = 0;
    cv::Size label_size =
        cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);

    int x = obj.rect.x;
    int y = obj.rect.y - label_size.height - baseLine;
    if (y < 0)
      y = 0;
    if (x + label_size.width > image.cols)
      x = image.cols - label_size.width;

    // the text of jack and drowning should be in different position
    cv::Point textPoint;
    if (detect_jack) {
      textPoint = cv::Point(x, y + 4 * label_size.height);
    } else {
      textPoint = cv::Point(x, y);
    }
    cv::rectangle(image,
                  cv::Rect(textPoint, cv::Size(label_size.width,
                                               label_size.height + baseLine)),
                  cv::Scalar(255, 255, 255), -1);

    if (detect_jack) {
      cv::putText(image, text, cv::Point(x, y + 5 * label_size.height),
                  cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
    } else {
      cv::putText(image, text, cv::Point(x, y + label_size.height),
                  cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
    }
  }

  return flag;
}